package com.shang.controller;

import com.shang.pojo.bo.ShopCartBO;
import com.shang.utils.JsonUtils;
import com.shang.utils.RedisOperator;
import com.shang.utils.ShangJSONResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Api(value = "购物车接口")
@RequestMapping("shopcart")
@RestController
public class ShopcatController extends BaseController{

    @Autowired
    private RedisOperator redisOperator;

    @ApiOperation(value = "同步购物车到后端")
    @PostMapping("/add")
    public ShangJSONResult add(@RequestParam String userId, @RequestBody ShopCartBO shopCartBO, HttpServletRequest request, HttpServletResponse response){

        if (StringUtils.isBlank(userId)){
            return ShangJSONResult.errorMsg("");
        }

        String shopcartJson = redisOperator.get(FOODIE_SHOPCART + ":" + userId);
        List<ShopCartBO> shopCartList = null;
        if (StringUtils.isNotBlank(shopcartJson)){
            //redis中已经有购物车了
            shopCartList = JsonUtils.jsonToList(shopcartJson,ShopCartBO.class);
            //判断购物车中是否存在已有商品，如果有则累加
            boolean isHaving = false;
            for (ShopCartBO sc : shopCartList){
                String tmpSpecId = sc.getSpecId();
                if (tmpSpecId.equals(shopCartBO.getSpecId())){
                    sc.setBuyCounts(sc.getBuyCounts() + shopCartBO.getBuyCounts());
                    isHaving = true;
                }
            }
            if (!isHaving){
                shopCartList.add(shopCartBO);
            }
        }else {
            //Redis中没有购物车数据
            shopCartList = new ArrayList<>();
            //直接添加到购物车中
            shopCartList.add(shopCartBO);
        }

        //覆盖现有redis中的购物车
        redisOperator.set(FOODIE_SHOPCART + ":" + userId,JsonUtils.objectToJson(shopCartList));


        return ShangJSONResult.ok();
    }


    @ApiOperation(value = "购物车删除")
    @PostMapping("/del")
    public ShangJSONResult del(@RequestParam String userId, @RequestParam String itemSpecId, HttpServletRequest request, HttpServletResponse response){

        if (StringUtils.isBlank(userId)  || StringUtils.isBlank(itemSpecId)){
            return ShangJSONResult.errorMsg("");
        }

        //用户在页面删除，如果登录，则同步后端
        String shopcartJson = redisOperator.get(FOODIE_SHOPCART + ":" + userId);
        if (StringUtils.isNotBlank(shopcartJson)){
            List<ShopCartBO> shopCartBOList = JsonUtils.jsonToList(shopcartJson, ShopCartBO.class);
            for (ShopCartBO sc : shopCartBOList){
                String tmpSpecId = sc.getSpecId();
                if (tmpSpecId.equals(itemSpecId)){
                    shopCartBOList.remove(sc);
                    break;
                }
            }
            redisOperator.set(FOODIE_SHOPCART + ":" + userId,JsonUtils.objectToJson(shopCartBOList));
        }

        return ShangJSONResult.ok();
    }

}
